package mapping

import (
	"pay/model"
	"pay/model/entity"
	"time"
)

// FeePlanPaymentOrdersEntityToDto entity data transfer
func FeePlanPaymentOrdersEntityToDto(feePlanPaymentOrders []*entity.FeePlanPaymentOrder) []*model.FeePlanPaymentOrderInfo {
	out := make([]*model.FeePlanPaymentOrderInfo, 0, len(feePlanPaymentOrders))
	for _, c := range feePlanPaymentOrders {
		out = append(out, FeePlanPaymentOrderEntityToDto(c))
	}
	return out
}

// FeePlanPaymentOrderEntityToDto entity data transfer
func FeePlanPaymentOrderEntityToDto(e *entity.FeePlanPaymentOrder) *model.FeePlanPaymentOrderInfo {

	var (
		fExpiredStatus = func(e *entity.FeePlanPaymentOrder) model.PayOrderStatus {
			var (
				s         = model.PayOrderStatus(e.Status)
				expiredAt = e.ExpiredAt
			)
			if s == model.OrderCreated && time.Now().Unix() > expiredAt {
				return model.OrderExpired
			}
			return s
		}
	)

	return &model.FeePlanPaymentOrderInfo{

		Id: e.Id,

		TradeNo: e.TradeNo,

		UserId: e.UserId,

		UserName: e.UserName,

		UserPhone: e.UserPhone,

		EbikeId: e.EbikeId,

		EbikeName: e.EbikeName,

		EbikeSnCode: e.EbikeSnCode,

		EbikeTypeId: e.EbikeTypeId,

		EbikeTypeName: e.EbikeTypeName,

		Status: int(fExpiredStatus(e)),

		StatusDetail: e.StatusDetail,

		Description: e.Description,

		FeePlanId: e.FeePlanId,

		FeePlanGroupId: e.FeePlanGroupId,

		FeePlanPackNo: e.FeePlanPackNo,

		Money: e.Money,

		CallBackCnt: e.CallBackCnt,

		Channel: e.Channel,

		CallBackData: e.CallBackData,

		TransactionId: e.TransactionId,

		PayAt: e.PayAt,

		ExpiredAt: e.ExpiredAt,

		CreatedAt: e.CreatedAt,

		UpdatedAt: e.UpdatedAt,
	}
}
